package com.css.fxfzxczh.modules.disasterSituationDataManage.samPoiHouSur.repository.impl;

import com.css.fxfzxczh.modules.disasterSituationDataManage.samPoiHouSur.entity.HouDam;
import com.css.fxfzxczh.modules.disasterSituationDataManage.samPoiHouSur.repository.HouseDamageRepository;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class HouseDamageRepositoryImpl implements HouseDamageRepository {
    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    @Override
    public boolean save(HouDam houDam) {
        String sql="insert into xczh_house_damage(id,structure_type,destroy,serious_damage,moderate_damage,minor_damage,basically_intact,xsphd_id,company) " +
                "values(:id, :structureType, :destroy, :seriousDamage, :moderateDamage, :minorDamage, :basicallyIntact, :xsphdId, :company)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(houDam);
        return namedParameterJdbcTemplate.update(sql, source)>0;
    }

    @Override
    public List<HouDam> findByXsphdId(String xsphdId) {
        String sql="select * from xczh_house_damage where xsphd_id = ?";
        List<HouDam> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(HouDam.class),xsphdId);
        return list;
    }

    @Override
    public void updateById(HouDam houDam) {
        String sql="update xczh_house_damage set structure_type =:structureType ,destroy =:destroy ," +
                "serious_damage =:seriousDamage ,moderate_damage =:moderateDamage ,minor_damage =:minorDamage ,basically_intact =:basicallyIntact ," +
                "company =:company where id =:id";
        SqlParameterSource source = new BeanPropertySqlParameterSource(houDam);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        namedParameterJdbcTemplate.update(sql, source,keyHolder);
    }

    /*@Override
    public List<HouDam> getDataByCompany(String company) {
        String sql="select * from xczh_house_Damage where company = ?";
        List<HouDam> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(HouDam.class),company);
        return list;
    }*/
}
